package com.sie.plm.rpm.standar.service.third.impl;

import cn.hutool.core.text.CharSequenceUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.rpm.service.remoterequest.ThirdRemoteRequestService;
import com.sie.plm.rpm.standar.service.third.ThirdApiService;
import com.sie.plm.rpm.standar.util.exception.StandarErrorCodeEnum;
import com.sie.plm.rpm.standar.vo.ProductVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.HashMap;
import java.util.Map;

@Service
public class ThirdApiServiceImpl implements ThirdApiService {
    private static final Logger log = LoggerFactory.getLogger(ThirdApiServiceImpl.class);
    @Autowired
    private ThirdRemoteRequestService remoteRequestService;

    @Override
    public String doPost(String apiKey, String param) {
        HttpHeaders httpHeaders = this.remoteRequestService.getHttpHeaders();
        HttpEntity<String> requestEntity = new HttpEntity(param, (MultiValueMap)httpHeaders);
        String result = this.remoteRequestService.getPostResponseBodyByApiKeyWords(apiKey, requestEntity);
        log.info("===========standar请求远程接口返回：" + result);
        if (CharSequenceUtil.isBlank(result) || CharSequenceUtil.isEmptyOrUndefined(result)) {
            throw StandarErrorCodeEnum.INTERFACE_NOT_ENABLED.message(new Object[] { apiKey });
        }
        JSONObject jsonObject = JSON.parseObject(result);
        if ("ERROR".equals(jsonObject.getString("result"))) {
            throw StandarErrorCodeEnum.REMOTE_EXCEPTION.message(new Object[] { jsonObject.getString("message") });
        }
        return result;
    }

    @Override
    public String getProductByOid(String oid) {
        Map<String, String> requstParam = new HashMap<>();
        requstParam.put("oid", oid);
        log.info("getProductByOid请求参数：" + JSON.toJSONString(requstParam));
        String result = doPost("tysoft.thirdRequest.getProductByOid", JSON.toJSONString(requstParam));
        log.info("getProductByOid返回结果：" + result);
        JSONObject jsonObject = JSON.parseObject(result);
        JSONObject data = jsonObject.getJSONObject("data");
        return JSON.toJSONString(data);
    }

    @Override
    public String getContainerTeamTree(ProductVO paramProductVO) {
        String result = doPost("tysoft.thirdRequest.getEnumerationTree", null);
        log.info("getEnumerationTree 返回结果: "+ result);
        JSONObject jsonObject = JSON.parseObject(result);
        JSONArray data = jsonObject.getJSONArray("data");
        return JSON.toJSONString(data);
    }

    @Override
    public String addMemberInRole(String requstParam) {
        log.info("addMemberInRole" + requstParam);
        String result = doPost("tysoft.thirdRequest.addMemberInRole", requstParam);
        log.info("addMemberInRole返回结果:" + result);
        return JSON.toJSONString(JSON.parseObject(result));
    }

    @Override
    public String getEnumItemsMapWithSort(String requstParam) {
        log.info("getEnumItemsMapWithSort请求:" + requstParam);
        String result = doPost("tysoft.thirdRequest.getEnumItemsMapWithSort", requstParam);
        log.info("getEnumItemsMapWithSort返回结果:" + result);
        JSONObject jsonObject = JSON.parseObject(result);
        JSONObject data = jsonObject.getJSONObject("data");
        return JSON.toJSONString(data);
    }

    @Override
    public String addRole(String requstParam) {
        log.info("addRole请求参数：" + requstParam);
                String result = doPost("tysoft.thirdRequest.addRole", requstParam);
        log.info("addRole返回结果：" + result);
        return JSON.toJSONString(JSON.parseObject(result));
    }

    @Override
    public String getEnumerationTree() {
        String result = doPost("tysoft.thirdRequest.getEnumerationTree", null);
        log.info("getEnumerationTree 返回：" + result);
        JSONObject jsonObject = JSON.parseObject(result);
        JSONArray data = jsonObject.getJSONArray("data");
        return JSON.toJSONString(data);
    }

    @Override
    public String insertEnumerationItem(String requstParam) {
        log.info("insertEnumerationItem 插入"  + requstParam);
        String result = doPost("tysoft.thirdRequest.insertEnumerationItem", requstParam);
        log.info("insertEnumerationItem 返回："+ result);
        return JSON.toJSONString(JSON.parseObject(result));
    }

    @Override
    public String deleteStandarProjectManager(String requstParam) {
        log.info("deleteStandarProjectManager :" + requstParam);
        String result = doPost("tysoft.thirdRequest.removeFromContainerTeam", requstParam);
        log.info("deleteStandarProjectManager 返回" + result);
        return JSON.toJSONString(JSON.parseObject(result));
    }

    @Override
    public JSONObject batchQueryDefByName(String requstParam) {
        log.info("batchQueryDefByName "+ requstParam);
        String result = doPost("tysoft.thirdRequest.batchQueryDefByName", requstParam);
        log.info("batchQueryDefByName " + result);
        JSONObject jsonObject = JSON.parseObject(result);
        JSONObject data = jsonObject.getJSONObject("data");
        return data;
    }

    @Override
    public String getSystemAndAttributesByType(String requstParam) {
        log.info("getSystemAndAttributesByType " + requstParam);
        String result = doPost("tysoft.thirdRequest.getSystemAndAttributesByType", requstParam);
        log.info("getSystemAndAttributesByType " + result);
        JSONObject jsonObject = JSON.parseObject(result);
        JSONObject data = jsonObject.getJSONObject("data");
        return JSON.toJSONString(data);
    }

    @Override
    public String getTypeAttrMappingsBySystem(String requstParam) {
        log.info("getTypeAttrMappingsBySystem" + requstParam);
        String result = doPost("tysoft.thirdRequest.getTypeAttrMappingsBySystem", requstParam);
        log.info("getTypeAttrMappingsBySystem "+ result);
        JSONObject jsonObject = JSON.parseObject(result);
        JSONArray data = jsonObject.getJSONArray("data");
        return JSON.toJSONString(data);
    }

    @Override
    public String getTypeByTypeName(String requstParam) {
        log.info("getTypeByTypeName" + requstParam);
        String result = doPost("tysoft.thirdRequest.getTypeByTypeName", requstParam);
        log.info("getTypeByTypeName" + result);
        JSONObject jsonObject = JSON.parseObject(result);
        JSONObject data = jsonObject.getJSONObject("data");
        return JSON.toJSONString(data);
    }

    @Override
    public String queryTypeAttr(String requstParam) {
        log.info("queryTypeAttr " + requstParam);
        String result = doPost("tysoft.thirdRequest.queryTypeAttr", requstParam);
        log.info("queryTypeAttr" + result);
        JSONObject jsonObject = JSON.parseObject(result);
        JSONArray data = jsonObject.getJSONArray("data");
        return JSON.toJSONString(data);
    }

    @Override
    public String saveObjIBAInfo(String requstParam) {
        log.info("saveObjIBAInfo "+ requstParam);
        String result = doPost("tysoft.thirdRequest.saveObjIBAInfo", requstParam);
        log.info("saveObjIBAInfo "+ result);
        return JSON.toJSONString(JSON.parseObject(result));
    }

    @Override
    public String updateObjIBAInfo(String requstParam) {
        log.info("updateObjIBAInfo" + requstParam);
                String result = doPost("tysoft.thirdRequest.updateObjIBAInfo", requstParam);
        log.info("updateObjIBAInfo "+ result);
        return JSON.toJSONString(JSON.parseObject(result));
    }

    @Override
    public String listUserByName(String requstParam) {
        log.info("listUserByName" + requstParam);
                String result = doPost("tysoft.thirdRequest.listUserByName", requstParam);
        log.info("listUserByName" + result);
                JSONObject jsonObject = JSON.parseObject(result);
        JSONArray data = jsonObject.getJSONArray("data");
        return JSON.toJSONString(data);
    }
}
